var model = require('../../model/model.js')
var show = false;
var item = {};
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appData:{},
    weatherKey : " ",
    item: {
      show: show
    },
    my: "未知",
    liveweather: {
      
    },
    three: [
    ],
    longitude: '',
    latitude: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getKey()
    
  },
  getData(){
    var that = this;
    that.setData({
      appData:app.data,
      // weatherKey:app.weatherKey
    })
    // var key=that.data.weatherKey
    wx.showLoading({
      title: '加载中',
    });
    console.log(that.data.weatherKey)
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        
        that.getCountry(longitude,latitude)
        that.getthree(longitude,latitude)
        that.getlive(longitude,latitude)
      }
    })
  },
  getKey() {
    let that = this
    wx.cloud.callFunction({
      name: 'getKey',
      data: {
        name: 'weather'
      }
    }).then(res => {
      console.log(res.result.data[0].key)
      that.setData({
        weatherKey: res.result.data[0].key
      })
      this.getData()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  //生命周期函数--监听页面初次渲染完成
  onReady: function (e) {
    var that = this;
    //请求数据
    model.updateAreaData(that, 0, e);
  },
  //点击选择城市按钮显示picker-view
  translate: function (e) {
    model.animationEvents(this, 0, true, 400);
  },
  getByCountry(name){
    var that=this
    wx.request({
      url: 'https://geoapi.qweather.com/v2/city/lookup?key='+that.data.weatherKey+"&location="+ name,
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        // var my = res.data.location[0].adm1 + " " + res.data.location[0].adm2 + " " + res.data.location[0].name
        // console.log(my)
        that.setData({
          longitude: res.data.location[0].lon,
          latitude: res.data.location[0].lat
        })
        that.getlive(res.data.location[0].lon,res.data.location[0].lat)
        that.getthree(res.data.location[0].lon,res.data.location[0].lat)
      }
    })
  },
  //隐藏picker-view
  hiddenFloatView: function (e) {
    model.animationEvents(this, 200, false, 400);
    var length = this.data.item.value[2];
    var lengthc = this.data.item.value[1];
    var lengthd = this.data.item.value[0];
    var countyCity = this.data.item.countys[length].name;
    var adm = this.data.item.countys[lengthc].name;
    if (countyCity == "市辖区"){
      countyCity = this.data.item.citys[lengthc].name;
      adm=this.data.item.countys[lengthd].name
    }

    var that = this;
    that.setData({
      my: that.data.province+" "+that.data.city+" "+countyCity
    })
    that.getByCountry(countyCity)
  },
  //滑动事件
  bindChange: function (e) {
    model.updateAreaData(this, 1, e);
    item = this.data.item;
    this.setData({
      province: item.provinces[item.value[0]].name,
      city: item.citys[item.value[1]].name,
      county: item.countys[item.value[2]].name
    });
  },
  onReachBottom: function () {
  },
  nono: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取天气
   */
  getlive: function (longitude, latitude) {
    var that=this
    wx.request({
      url: 'https://devapi.qweather.com/v7/weather/now?key='+that.data.weatherKey+'&location=' + longitude + ',' + latitude,
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          // my: my,
          liveweather: res.data.now
        })
        
      }
    })
  },
  getthree: function (longitude, latitude) {
    var that=this
    wx.request({
      url: 'https://devapi.qweather.com/v7/weather/3d?key='+this.data.weatherKey+'&location=' + longitude + ',' + latitude,
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res1) {
        wx.hideLoading();
        console.log(res1)
        that.setData({
          three: res1.data.daily
        })
      }
    })
  },
  getCountry: function(longitude,latitude){
    var that=this
    wx.request({
      url: 'https://geoapi.qweather.com/v2/city/lookup?key='+that.data.weatherKey+"&location="+ longitude + ',' + latitude,
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        var my = res.data.location[0].adm1 + " " + res.data.location[0].adm2 + " " + res.data.location[0].name
        console.log(my)
        that.setData({
          my: my,
          // liveweather: res.data.now
        })
      }
    })
  },
  formateDate(date){
    // return date.substring(0,3)
    return "hubu"
  }
})